import { Module } from "vuex";

import { IRootState } from "../types";
import { IRecommendState } from "./types";

import { 
  getTopBanners,
  getHotRecommend,
  getNewAlbums,
  getRanking,
  getArtistList
} from "@/service/recommend";

const recommendModule: Module<IRecommendState, IRootState> =  {
  namespaced: true,
  state() {
    return {
      topBanners: [],
      bgImage: "",
      hotRecommends: [],
      newAlbums: [],
      upRanking: {},
      originRanking: {},
      newRanking: {},
      settleSingers: []
    }
  },
  mutations: {
    changeTopBanners(state, list) {
      state.topBanners = list
    },
    changeBgImage(state, img) {
      state.bgImage = img + "?imageView&blur=40x20"
    },
    changeHotRecommends(state, list) {
      state.hotRecommends = list
    },
    changeNewAlbums(state, list) {
      state.newAlbums = list
    },
    changeUpRanking(state, obj) {
      state.upRanking = obj
    },
    changeOriginRanking(state, obj) {
      state.originRanking = obj
    },
    changeNewRanking(state, obj) {
      state.newRanking = obj
    },
    changeSettleSingers(state, list) {
      state.settleSingers = list
    }
  },
  actions: {
    async getTopBannersAction({ commit }) {
      const res = await getTopBanners();
      const imgUrl = res.banners[0].imageUrl
      commit("changeTopBanners", res.banners)
      commit("changeBgImage", imgUrl)
    },
    async getHotRecommendAction({commit}, limit: number) {
      const res = await getHotRecommend(limit);
      commit("changeHotRecommends", res.result)
    },
    async getNewAlbumsAction({commit}, limit: number) {
      const res = await getNewAlbums(limit);
      commit("changeNewAlbums", res.albums)
    },
    async getRankingAction({commit}, idx: number) {
      const res = await getRanking(idx);
      switch (idx) {
        case 3:
          commit("changeUpRanking", res.playlist)
          break;
        case 2:
          commit("changeOriginRanking", res.playlist)
          break;
        case 0:
          commit("changeNewRanking", res.playlist)
          break;
        default:
          break;
      }
    },
    async getSettleSingersAction({ commit }, payload: any) {
      const { limit, cat } = payload;
      const res = await getArtistList(limit, cat);
      commit("changeSettleSingers", res.artists)
    }
  }
}

export default recommendModule